﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Saker.Utility;
using Saker.Kernel.BLL;
using Saker.Kernel.Model;

namespace Saker.Web.Boss.Sys.Depart
{
    /// <summary>
    /// DepartAdd 的摘要说明
    /// </summary>
    public class DepartAdd : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "application/json";
            string cmd = context.Request["CMD"].ToString();
            if (cmd == "add")
            {
                string parentId = context.Request["parentId"].ToString();
                string name = context.Request["name"].ToString();
                if (new SysDepartmentBLL().Add(int.Parse(parentId), name))
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 1, message = "部门添加成功！" }));
                }
                else
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 0, message = "部门添加失败！" }));
                }
            }
            if (cmd == "edit")
            {
                string departId = context.Request["id"].ToString();
                string name = context.Request["name"].ToString();
                SysDepartment depart = new SysDepartmentBLL().GetByDepartID(int.Parse(departId));
                depart.DepartName = name;
                if (new SysDepartmentBLL().Update(depart) > 0)
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 1, message = "部门修改成功！" }));
                }
                else
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 0, message = "部门修改失败！" }));
                }
            }
            if (cmd == "del")
            {
                string[] ids = context.Request["ids"].ToString().Split(',');
                int count = 0;
                foreach (string id in ids)
                {
                    count += new SysDepartmentBLL().DeleteByDepartID(int.Parse(id));
                }
                if (count > 0)
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 1, message = "部门删除成功！" }));
                }
                else
                {
                    context.Response.Write(JsonHelper.objectToJson(new { code = 0, message = "部门删除失败！" }));
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}